package com.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Shop_model;

/**
 * Servlet implementation class Shop_ser
 */
@WebServlet("/Shop_ser")
public class Shop_ser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Shop_ser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	      HttpSession session = request.getSession(true);
	      response.setContentType("text/html;charset=utf-8");
	       response.setCharacterEncoding("utf-8");
	       String price = (String) request.getParameter("Price");
	       String name  = request.getParameter("name");
	  
	       System.out.println(price);
	       System.out.println("servlet执行了...");
	       
	       if(session.getAttribute("list")==null){
	    	   List<Shop_model> list = new ArrayList<>();
	    	   Shop_model model = new Shop_model();
	    	   model.setName(name);
	    	   model.setPrice(price);
	    	   list.add(model);
	    	   session.setAttribute("list", list);
	       }else{
	    	   List<Shop_model> list = (List<Shop_model>) session.getAttribute("list");
	    	   Shop_model model = new Shop_model();
	    	   model.setName(name);
	    	   model.setPrice(price);
	    	   list.add(model);
	    	   session.setAttribute("list",list);	    	  
	       }
	       List<Shop_model> list = (List<Shop_model>) session.getAttribute("list");
	       
	       session.setAttribute("Price",price );
	       response.getWriter().write("您添加的物品价格为："+price+"元");
	       request.getRequestDispatcher("jiesuan.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doGet(request, response);
	}

}
